__author__ = 'drazen.popovic'

DATETIME_FORMAT = "%Y%m%d%H%M"
TIMESTAMP_FORMAT = DATETIME_FORMAT + "%S"

fields = [
    {"name" : "action_sign", "type" : "int", "fmt": "02d", "size": 2},
    {"name" : "imsi", "type" : "int", "fmt": "02d", "size": 2},
    {"name" : "msisdn", "type" : "str", "fmt": "s", "size": 12},
    {"name" : "gk_id", "type" : "str", "fmt": "s", "size": 15},
    {"name" : "ipt_id", "type" : "str", "fmt": "s", "size": 9},
    {"name" : "status_code", "type" : "int", "fmt": "02d", "size": 2},
    {"name" : "sms_datetime", "type" : "datetime", "fmt": "s", "size": 12},
    {"name" : "enterence_datetime", "type" : "datetime", "fmt": "s", "size": 12},
    {"name" : "pay_datetime", "type" : "datetime", "fmt": "s", "size": 12},
    {"name" : "leave_datetime", "type" : "datetime", "fmt": "s", "size": 12},
    {"name" : "exit_datetime", "type" : "datetime", "fmt": "s", "size": 12},
    {"name" : "amount", "type" : "float", "fmt": "06d", "size": 6},
    {"name" : "currency", "type" : "str", "fmt": "s", "size": 3},
    {"name" : "ipt_id_list", "type" : "str", "fmt": "s", "size": 50},
    {"name" : "timestamp", "type" : "datetime", "fmt": "s", "size": 17},
]

def FormName(name):
    sp = name.split("_")
    s = ""
    for n in sp:
        n = n.capitalize()
        s += n
    return s

def DumpAttributes():
    for field in fields:
        if field["type"] == "int":
            print "_{0} = IntField(None)".format(field["name"])
        if field["type"] == "str":
            print "_{0} = StringField(None)".format(field["name"])
        if field["type"] == "float":
            print "_{0} = FloatField(None)".format(field["name"])
        if field["type"] == "datetime":
            print "_{0} = DatetimeField(None)".format(field["name"])


def DumpWriters():
    for field in fields:
        name = field["name"]
        type = field["type"]
        fmt = field["fmt"]
        size = field["size"]
        formated_name = FormName(name)
        print "def write{0}(self, stream):".format(formated_name)
        print "\ttell_start = stream.tell()"
        print "\ttry:"
        print "\t\tvalue = self.get{0}()".format(formated_name)
        print "\t\tstream.write(\"\\t{{0:{0}}}\".format(value)[:{1}])".format(fmt, size)
        print "\texcept ValueError:"
        print "\t\tstream.write(\"\\t\")"
        print "\ttell_end = stream.tell()"
        print "\treturn tell_end - tell_start"
    print "def writeToStream(self, stream):"
    print "\ttell_start = stream.tell()"
    for field in fields:
        name = field["name"]
        type = field["type"]
        fmt = field["fmt"]
        size = field["size"]
        formated_name = FormName(name)
        print "\tself.write{0}(stream)".format(formated_name)
    print "\tstream.write(\"\\r\\n\")"
    print "\ttell_end = stream.tell()"
    print "\treturn tell_end - tell_start"
def DumpReaders():
    for field in fields:
        name = field["name"]
        type = field["type"]
        fmt = field["fmt"]
        size = field["size"]
        formated_name = FormName(name)
        print "def read{0}(self, stream):".format(formated_name)
        print "\tdelimiters = [\"\\t\"]"
        print "\tstrField = readField(stream, delimiters)"
        print "\tif strField == \"\":"
        print "\t\tvalue = None"
        print "\telse:"
        if type == "int":
            print "\t\tvalue = int(strField)"
        if type == "str":
            print "\t\tvalue = strField"
        if type == "datetime":
            print "\t\tvalue = datetime.strptime(strField, self.DATETIME_FORMAT)"
        if type == "float":
            print "\t\tvalue = float(int(strField))/100.0"
        print "\tself.set{0}(value)".format(formated_name)
        print "\treturn value"
    print "def readFromStream(self, stream):"
    for field in fields:
        name = field["name"]
        type = field["type"]
        fmt = field["fmt"]
        size = field["size"]
        formated_name = FormName(name)
        print "\tself.read{0}(stream)".format(formated_name)
    print "\treturn self"

def DumpSetters():
    for field in fields:
        print "def set{0}(self, {1}):".format(FormName(field["name"]), field["name"])
        print "\tself.validate{0}({1})".format(FormName(field["name"]), field["name"])
        print "\tself._{0}.setValue({0})".format(field["name"])

def DumpGetters():
    for field in fields:
        print "def get{0}(self):".format(FormName(field["name"]))
        print "\treturn self._{0}.getValue()".format(field["name"])

def DumpValidators():
    for field in fields:
        print "def validate{0}(self, {1}):".format(FormName(field["name"]), field["name"])
        print "\tpass"

def DumpFormaters():
    for field in fields:
        print "\"{0}:{{0:s}}\\n\".format(str(self._{1}._value)) + \\".format(field["name"].upper(),field["name"])

#DumpWriters()
#DumpReaders()
#DumpAttributes()
#DumpSetters()
#DumpGetters()
#DumpValidators()
DumpFormaters()
